<template>
	<custom-navbar title="查看反馈"></custom-navbar>
	<view class="feedback">
		<book-link :fileId="fileId"></book-link>
		<!-- 文件上传模块-->
		<view class="controll">
			<view class="staticAdd">
				<view class="title">
					<text>选择反馈领域</text>
				</view>
				<view class="feedItemList" v-for="(item,index) in feedBackList" :key="index" @click="skipItemDetail(item)">
					<text>{{item.dictLabel}}</text>
					<view class="feed_right">
					<text v-show="item.isWrite">已填写反馈</text>
					<image src="../../static/icon/arrow_right.png" mode=""></image>	
					</view>
				</view>
			</view>
			<view class="title">
				<text>其他描述</text>
			</view>

			<view class="textAreaWrap">
				<textarea v-model="descript" placeholder="请填写文字描述"  ></textarea>
			</view>
			<view class="title required">
				<text>回函</text>
			</view>
			<process-wrap :fileListInfo="fileListInfo[0]" :documentType="0">
			</process-wrap>
			<view class="title required">
				<text>是否做出行政处罚</text>
				<uni-data-checkbox v-model="publish" :localdata="sexs" />
			</view>
			<custom-button @tap="submitForm">确认提交</custom-button>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import {onLoad,onShow} from '@dcloudio/uni-app'
import {getFeedbackFieldList,alreadyFeedbackData,addFeedbackData} from '@/api/document.js'
import {useProcessStore} from '@/store/index.js'
import {getSaveRole} from '@/utils/auth.js'
const processStore = useProcessStore()
const fileId = ref("")
const fileListInfo = [
	[{uploadType:"file"},{uploadType:"image"}],
	[]
]
onLoad((data)=>{
	fileId.value = data.id
	processStore.clearProcess()
})
onShow(()=>{
	getFeedbackItemList()
})
const feedBackList = ref([])
uni.setNavigationBarTitle({
	title:"检察意见反馈"
})
const sexs = [{text: '否',value: 0}, {text: '是',value: 1}]
const descript = ref("")
const publish = ref(0)
// 前往细节填写
const skipItemDetail = (item)=>{
	let url = ""
	if(item.isWrite){
		url =  `/pages/seeFeedBack/seeFeedBack?deliveryId=${fileId.value}&field=${item.dictValue}`
	}else{
		url=`/pages/feedbackArea/feedbackArea?areaName=${item.dictLabel}&field=${item.dictValue}&id=${fileId.value}`
	}
	uni.navigateTo({
		url
	})
}
// 获取反馈领域列表
const getFeedbackItemList = async()=>{
const markList = await alreadyList()
const {code,data:res} = await getFeedbackFieldList()
if(code === 200){
	feedBackList.value = res.map(val=>{
		return {...val,isWrite:markList.includes(val.dictValue)}
	})
}
}
// 获取已经反馈的列表
const alreadyList = ()=>{
return new Promise(async(resolve,reject)=>{
	const {code,data:res} = await alreadyFeedbackData({
		deliveryId:fileId.value 
	})
	if(code === 200){
		resolve(res)
	}else{
		resolve([])
	}
})

}
// 提交反馈信息
const submitForm = async()=>{
    if(processStore.processPingList[0].length === 0){
		return uni.showToast({
			icon:'error',
			title:"请上传回函！"
		})
	}
 const {code,data:res} = await addFeedbackData({
	 deliveryId:fileId.value,
	 fileOssIds:processStore.processPingList[0].reduce((curr,val)=>{
		 curr.push(val.ossId)
		 return curr
	 },[]),
	 replyOthers:descript.value,
	 isAdminiPenalized:publish.value
 })
 if(code === 200){
	 if(res){
		 uni.showToast({
		 	icon:"success",
			title:"提交成功",
			duration:1000,
			complete() {
				const role = getSaveRole()
				let url = ""
				switch(role){
					case 'Procuratorate': //检察院
					uni.switchTab({
						url: "/pages/index/index"
					})
					break;
					case 'AdministrativeAgencies': //行政机关
					uni.navigateTo({
						url:"/pages/administrativeAgencies/administrativeAgencies"
					})
					break;
				}

			}
		 })
	 }else{
		uni.showToast({
			icon:"error",
			title:"提交失败"
		})
	 }
 }else{
	 uni.showToast({
	 	icon:"error",
	 	title:"提交失败"
	 })
 }
}
</script>

<style lang="scss" scoped>
.textAreaWrap{
	height: 264rpx;
	background: #F5F9FF;
	border-radius: 8rpx;
	border: 1rpx solid #ECEFF4;
	padding: 32rpx;
}
.controll{
	padding: 0 32rpx;
	margin-bottom: 50rpx;
	.feedItemList{
		display: flex;
		justify-content: space-between;
		padding: 34rpx 0;
		border-bottom: 2rpx solid #F5FAFF;
		font-weight: 400;
		>text{
			font-size: 28rpx;
			color: #44474A;
		}
		.feed_right{
			display: flex;
			align-items: center;
			>image{
				width: 24rpx;
				height: 24rpx;
			}
			>text{
				font-size: 24rpx;
				color: #BBC0C9;
				margin-right: 16rpx;
			}
		}
	}
}
.feedback{
	padding:35rpx;
	.title{
		font-weight: 600;
		font-size: 32rpx;
		color: #00041A;
		margin: 32rpx 0;
	}
	.required{
		>text{
		display: inline-block;
		position: relative;
		&::after{
			position: absolute;
			content: "*";
			right: -20rpx;
			top: 50%;
			transform: translateY(-50%);
			color: #FF4122;
		}	
		}

	}
	}
	

	:deep(.uni-textarea){
		width: 100%;
		height: 100%;
	}
	:deep(.checklist-group){
		margin: 30rpx 0 80rpx;
	}
</style>
